﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Actions;

namespace Eacademy.Areas.Admin.Controllers
{
    [CustomAuthorize(Roles = "Admin")]
    public class QuizzesController : Controller
    {
        private const int DefaultPageSize = 10;

        private IQuizRepository _quizRepsitory;
        private ISubjectRepository _subjectRepository;
        private IMentorRepository _mentorRepository;

        public QuizzesController(IQuizRepository quizRepository, ISubjectRepository subjectRepository, IMentorRepository mentorRepository)
        {
            _quizRepsitory = quizRepository;
            _subjectRepository = subjectRepository;
            _mentorRepository = mentorRepository;

        }

        //
        // GET: /Admin/Quizzes/

        public ActionResult Index(int id, int? page, int mentoruserid = 0)
        {
            if (mentoruserid < 1)
            {
                return Redirect("/admin/subjects");
            }
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            QuizListViewModel quizListViewModel = new QuizListViewModel();
            int totalcount = 0;
            List<QuizViewModel> quizViewModels = _quizRepsitory.GetQuizzesBySubjectId(id, mentoruserid,
                                                                                      currentPageIndex + 1,
                                                                                      DefaultPageSize, out totalcount);
            AdminSubjectsViewModel adminSubjectsViewModel = _subjectRepository.GetSubjectBySubjectId(id);
            AdminMentorsViewModel mentor = _mentorRepository.GetMentorByUserId(mentoruserid);

            quizListViewModel.SubjectName = adminSubjectsViewModel.SubjectCode;
            quizListViewModel.SubjectId = id;
            quizListViewModel.MentorUserId = mentoruserid;
            quizListViewModel.MentorId = mentor.MentorId;
            quizListViewModel.QuizViewModelList = quizViewModels.ToPagedList(currentPageIndex,DefaultPageSize,totalcount);

            return View(quizListViewModel);
        }

        [HttpPost]
        public ActionResult Add(int id, QuizViewModel quizViewModel,int mentoruserid)
        {
            _quizRepsitory.AddQuiz(quizViewModel,id, mentoruserid);

            //var choices = new Choice[]{
            //    new Choice{ ChoiceAnswer = "Test",ChoiceId = 1,IsAnswer = false}
            //};
            //QuestionViewModel questionViewModel = new QuestionViewModel();

            //questionViewModel.Choices = choices.ToList();

            return
                Redirect(Url.Action("Index",
                                    new {Controller = "Quizzes", Area = "Admin", id = id, mentoruserid = mentoruserid}));

        }


        public ActionResult Edit(int id,int mentoruserid)
        {
            QuizViewModel quizViewModel = _quizRepsitory.GetQuizByQuizId(id);
            quizViewModel.MentorUserId = mentoruserid;
            quizViewModel.QuizId = id;
            return View(quizViewModel);
        }

        [HttpPost]
        public ActionResult Edit(QuizViewModel quiz,int mentoruserid)
        {
            if (_quizRepsitory.UpdateQuiz(quiz))
            {
                this.FlashDone("The quiz has been updated");
            }
            else
            {
                this.FlashError("There was an error updating the quiz");
            }
            return
                   Redirect(Url.Action("Index",
                                       new { Controller = "Quizzes", Area = "Admin", id = quiz.QuizId, mentoruserid = mentoruserid }));

        }

        [HttpPost]
        public ActionResult Delete(int id,int mentorId)
        {
            if (_quizRepsitory.DeleteQuiz(id,mentorId))
            {
                this.FlashDone("Quiz deleted");
            }
            else
            {
                this.FlashError("Quiz was not deleted");
            }

            return Content("deleted");
        }

    }
}
